<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="项目名称" prop="location">
        <el-select
          v-loadMore="loadMore"
          style="width: 198px" v-model="queryParams.location" clearable remote
          :remote-method="remoteMethod" filterable placeholder="请选择"
        >
          <el-option v-for="dict in optionsitem" :key="dict.value" :label="dict.label" :value="dict.label">
            <span style="float: left">{{ dict.label }}</span>
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="课题号" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入课题号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目类别" prop="projectCategory">
        <el-select v-model="queryParams.projectCategory" placeholder="请选择项目类别" clearable>
          <el-option
            v-for="dict in dict.type.village_project_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目级别" prop="projectMold">
        <el-select v-model="queryParams.projectMold" placeholder="请选择项目级别" clearable>
          <el-option
            v-for="dict in dict.type.village_achievement_rank"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主持人" prop="programLeader">
        <el-input
          v-model="queryParams.programLeader"
          placeholder="请输入主持人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立项时间" prop="projectApprovalTime">
        <el-date-picker clearable
                        v-model="queryParams.projectApprovalTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择立项时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年度查询" prop="yearprojectApprovalTime">
        <div class="block">
          <el-date-picker
            v-model="datavalue"
            type="year"
            placeholder="选择年">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="$router.push(`/Pedagogicalchange`)"
          v-hasPermi="['village:pedagogical:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate()"
          v-hasPermi="['village:pedagogical:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['village:pedagogical:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['village:pedagogical:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reformList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="location" />
      <el-table-column label="项目主持人" align="center" prop="programLeader" />
      <el-table-column label="单位名称" align="center" prop="nameOfWorkingUnit" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="立项时间" align="center" prop="projectApprovalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectApprovalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目经费" align="center" prop="projectFund" />
      <el-table-column label="剩余经费" align="center" prop="balance" />
      <el-table-column label="项目状态" align="center" prop="projectStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_project_status" :value="scope.row.projectStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="skipexpenditreDetail(scope.row)"
            v-hasPermi="['village:pedagogical:edit']">记账支出
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['village:pedagogical:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="skipDetail(scope.row)"
            v-hasPermi="['village:pedagogical:edit']">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['village:pedagogical:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listReform, getReform, delReform, addReform, updateReform } from '@/api/village/reform'

export default {
  name: 'Reform',
  dicts: ['village_project_status', 'village_achievement_rank', 'villages_project_type', 'village_project_category', 'village_ongoing_development'],
  data() {
    return {
      isget:true,
      allItems: [],
      restaurants: [],
      expenselist: [],
      options: [],
      optionsitem: [],
      pageData: {
        pageNum: 1,
        pageSize: 10
      },
      pagetotal:0,
      loadingMore: false,
      date: '',
      datavalue: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教改项目表格数据
      reformList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        location:null,
        supportingFundsType: null,
        taskId: null,
        type: null,
        projectCategory: null,
        projectMold: null,
        projectApprovalTime:null,
        yearprojectApprovalTime:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: '项目类型不能为空', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        programLeader: [
          { required: true, message: '项目主持人不能为空', trigger: 'blur' }
        ],
        projectApprovalTime: [
          { required: true, message: '立项时间不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: false  },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    loadMore() {
      this.pageData.pageNum++;

      if (this.list.length !== this.pagetotal){
        listReform(this.pageData).then(response => {
          let relist = response.rows
          this.pagetotal = response.total
          this.options = relist.map(r => {
            return {
              label: r.location,
              value: r.id
            }
          })
          this.states = this.options.map(item => item.label)
          this.options.forEach(option => {
            this.list.push(option);
          });

        })
      }

      // this.sceneData(this.pageData);
    },
    remoteMethod(query) {

      if (query !== '') {
        this.loadings = true // 注意这里是 this.loadings
        this.optionsitem = []
        this.queryParams.location = query
        listReform({ location: query }).then(response => {
          let rlist = response.rows
          this.optionsitem = rlist.map(r => {
            return {
              label: r.location,
              value: r.id
            }
          })
        })

        this.loadings = false
      } else {
        this.optionsitem =  this.list
      }
    },
    skipexpenditreDetail(row) {
      this.$router.push(`/pedagogicalexpenditure?id=${row.id}`)
    },
    /** 查询教改项目列表 */
    getList() {


      this.loading = true
      listReform(this.queryParams).then(response => {
        this.reformList = response.rows
        this.total = response.total

        if(this.optionsitem.length<1){
          this.optionsitem = this.reformList.map(r => {
            return {
              label: r.location,
              value: r.id
            }
          })
          // this.states = this.options.map(item => item.label)
          this.list = this.optionsitem
        }
        // this.optionsitem = this.options
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        location: null,
        programLeader: null,
        type: null,
        projectCategory: null,
        projectMold: null,
        nameOfWorkingUnit: null,
        mailingAddress: null,
        postalCode: null,
        phone: null,
        eMail: null,
        projectApprovalTime: null,
        projectFund: null,
        schoolSupportingFunds: null,
        citySupportingFunds: null,
        balance: null,
        supportingFundsType: null,
        beginTime: null,
        cutOffTime: null,
        resignationLetter: null,
        task: null,
        proposalMaterial: null,
        interimReport: null,
        finishingMaterials: null,
        supportingMaterial: null,
        studyReport: null,
        changeOfMaterial: null,
        createTime: null,
        projectStatus: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        isDelete: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.datavalue != null){
        let year = this.datavalue.getFullYear()
        this.queryParams.yearprojectApprovalTime = year
        console.log("year:"+year)
      }

      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.datavalue=null
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加教改项目'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      if (row != null) {
        this.$router.push(`/Pedagogicalchange?id=${row.id}`)
      } else {
        this.$router.push(`/Pedagogicalchange?id=${this.ids[0]}`)
      }


    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReform(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addReform(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除教改项目名称为"' + row.location + '"的数据项？').then(function () {
        return delReform(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('village/reform/export', {
        ...this.queryParams
      }, `reform_${new Date().getTime()}.xlsx`)
    },
    // 详情
    skipDetail(row) {
      this.$router.push(`/Pedagogicaldetail?id=${row.id}`)
    }
  }
}
</script>
